<template>
  <div class="register">
    <div class="login-con">
      <el-form
        ref="formInline"
        :model="formInline"
        :rules="ruleInline"
        label-width="100px"
        @submit.native.prevent
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="user">
          <el-input type="text" v-model="formInline.username" placeholder="Username"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input type="password" v-model="formInline.password" placeholder="Password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱验证码" prop="emailCode">
          <el-input type="text" v-model="formInline.emailCode" placeholder="code"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input type="text" v-model="formInline.email" placeholder="Email"></el-input>

          <br />
          <input type="submit" value="发送" @click="checkEmailCode()" style="float: left;" />
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="reSetPasswordByEmail" align>修改密码</el-button>
    </div>
  </div>
</template>
<script>
import { resolve } from "q";
export default {
  data() {
    return {
      formInline: {
        username: "",
        password: "",
        email: "",
        emailCode: ""
      },

      checkcode: "",
      ruleInline: {
        user: [
          {
            message: "Please fill in the user name",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "Please fill in the password.",
            trigger: "blur"
          }
        ],
        email: [
          {
            required: true,
            message: "Please fill in the email",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    checkEmailCode: function() {
      var data = this.formInline.email;
      var url = "/ssms/EmailCode";
      this.$axios
        .get(
          url,
          { params: { toEmail: data } },
          {
            headers: {
              "Content-Type": "application/json"
            }
          }
        )
        .then(response => {
          console.log(response.data);
          if (response.data.code === 0) {
            alert(response.data.obj);
            alert("添加成功！");
          } else {
            alert(response.data.obj);
          }
        })
        .catch(error => {
          alert(error);
        });
    },
    reSetPasswordByEmail: function() {
      this.$axios
        .get(
          "/ssms/reSetPasswordByEmail",
          {
            params: {
              emailCode: this.formInline.emailCode,
              newPassword: this.formInline.password,
              username: this.formInline.username
            }
          },
          {
            headers: {
              "Content-Type": "application/json",
              responseType: "application/json"
            }
          }
        )
        .then(res => {
          console.log(res.data);
          if (res.data.code === 0) {
            alert(res.data.msg);
            alert(res.data.obj);
            this.$router.push("/userView/rooms");
          } else {
            alert(res.data.msg);
            alert(res.data.obj);
          }
        });
    }
  }
};
</script>

<style>
</style>
